/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * vwEngineer.java
 *
 * Created on 18-mei-2011, 14:25:42
 */
package tcnplanner;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author Koen
 */
public class vwEngineer extends javax.swing.JPanel {

    private Controller controller;
    private Reservation selected = null;
    
    /** Creates new form vwOperator */
    public vwEngineer() {
        initComponents();
        controller = Main.controller;
        this.tableReservations.setAutoCreateRowSorter(true);
        this.tableReservations.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent lse) {                
                selected = (Reservation) tableReservations.getModel().getValueAt(tableReservations.getSelectedRow(), 0);
                vwInfo1.setReservation(selected);
            }
        });
        loadReservations();
    }
    
    private void loadReservations() {
        ArrayList<Reservation> resengineer = new ArrayList<Reservation>();
        Iterator it =controller.getAllReservations().iterator();
        while(it.hasNext()){
            Reservation next = (Reservation)it.next();
            if(next.getStatus() == Reservation.statusType.SCHEDULED || next.getStatus() == Reservation.statusType.CONSTRUCTING ){
            resengineer.add(next);
            }
        }
        TableModel model =  new ReservationTable(resengineer);
        tableReservations.setModel(model);       
    }
    
    @Action
    public void updateReservationStatus(){
        if(selected != null)
            switch(cbStatustypes.getSelectedIndex())
            {
                case 0:
                    selected.setStatus(Reservation.statusType.CONSTRUCTING);
                    break;
                case 1:
                    selected.setStatus(Reservation.statusType.WAITING);
                    break;
                    
            }
        controller.editReservation(selected);
        loadReservations();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jReserveringen = new javax.swing.JLabel();
        vwInfo1 = new tcnplanner.vwInfo();
        cbStatustypes = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btUpdateStatus = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableReservations = new javax.swing.JTable();

        setBackground(java.awt.SystemColor.inactiveCaption);

        jPanel2.setBackground(java.awt.SystemColor.inactiveCaption);
        jPanel2.setName("jPanel2"); // NOI18N

        jReserveringen.setText("Reserveringen:");
        jReserveringen.setName("jReserveringen"); // NOI18N

        vwInfo1.setName("vwInfo1"); // NOI18N

        cbStatustypes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CONSTRUCTING", "WAITING" }));
        cbStatustypes.setName("cbStatustypes"); // NOI18N

        jLabel1.setText("Status");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(vwEngineer.class, this);
        btUpdateStatus.setAction(actionMap.get("updateReservationStatus")); // NOI18N
        btUpdateStatus.setName("btUpdateStatus"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tableReservations.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableReservations.setName("tableReservations"); // NOI18N
        jScrollPane1.setViewportView(tableReservations);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jReserveringen)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vwInfo1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btUpdateStatus, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(cbStatustypes, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jReserveringen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(vwInfo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(cbStatustypes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(btUpdateStatus)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 479, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 22, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 23, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 591, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 40, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 41, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btUpdateStatus;
    private javax.swing.JComboBox cbStatustypes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel jReserveringen;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableReservations;
    private tcnplanner.vwInfo vwInfo1;
    // End of variables declaration//GEN-END:variables
}
